package com.gl.ruanzhu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.gl.ruanzhu.mapper.*;
import com.gl.ruanzhu.model.*;
import com.gl.ruanzhu.outDto.NameMax;
import com.gl.ruanzhu.outDto.TaskOutDto;
import com.gl.ruanzhu.outDto.Times;
import com.gl.ruanzhu.service.IScienceServicesDalService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ScienceServicesDalServiceImpl implements IScienceServicesDalService {

    @Autowired
    private ITaskMapper taskMapper;
    @Autowired
    private ITaskSupplierMapper taskSupplierMapper;
    @Autowired
    private IContractMapper contractMapper;
    @Autowired
    private ITaskPlanMapper taskPlanMapper;
    @Autowired
    private ITaskPersonMapper taskPersonMapper;
    @Autowired
    private ITaskDesignMapper taskDesignMapper;
    @Autowired
    private ICirculationListMapper circulationListMapper;

    @Override
    public List<Task> getALL(Map<String, Object> params) {
        return taskMapper.getAll(params);
    }


    @Override
    public List<Task> showDesignSon(Map<String, Object> params) {
        return taskMapper.showDesignSon(params);
    }

    @Override
    public TaskOutDto getDetails(Map<String, Object> params) {
        return taskMapper.getDetails(params);
    }

    @Override
    public List<TaskSupplier> getSuppliers(Map<String, Object> params) {
        return taskSupplierMapper.getAll(params);
    }

    @Override
    public List<Contract> getContract(Map<String, Object> params) {
        return contractMapper.getAll(params);
    }

    @Override
    public List<TaskOutDto> getUnResponded(Map<String, Object> params) {
        return taskMapper.getUnResponded(params);
    }

    @Override
    public List<TaskPlan> getPlanState(Map<String, Object> params) {
        return taskPlanMapper.getAll(params);
    }
    @Override
    public List<TaskPerson> getPerson(Map<String, Object> params) {
        return taskPersonMapper.getAll(params);
    }

    @Override
    public List<TaskDesign> getDesign(Map<String, Object> params) {
        return taskDesignMapper.getAll(params);
    }

    @Override
    public List<CirculationList> getCirculationList(Map<String, Object> params) {
        return circulationListMapper.getAll(params);
    }

    @Override
    public Times getTimes(Map<String, Object> params) {
        Times t = new Times();
        long[] valueList= new long[4];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss");
        //申请时长
        List<TaskSupplier> supplierList = taskSupplierMapper.getAll(params);
        supplierList.forEach(s->{
            if(s.getAuditStatus()==1){
                if(ObjectUtil.isNotEmpty(s.getAuditStatus()) && ObjectUtil.isNotEmpty(s.getJoinTime())) {
                    t.setApplyTime(s.getAuditTime().getTime() - s.getJoinTime().getTime());
                    valueList[0]=t.getApplyTime();
                }
            }
        });
        //计划时长
        List<TaskDesign> taskDesignList = taskDesignMapper.getAll(params);
        taskDesignList.forEach(s->{
            if(s.getAuditStatus()==1){
                if(ObjectUtil.isNotEmpty(s.getAuditStatus()) && ObjectUtil.isNotEmpty(s.getUploadTime())) {
                    t.setPlanTime(s.getAuditTime().getTime() - s.getUploadTime().getTime());
                    valueList[1]=t.getPlanTime();
                }
            }
        });
        //任务时长和审核时长
        List<TaskDesign> taskPlanList = taskDesignMapper.getAll(params);
        taskPlanList.forEach(s->{
            if(s.getAuditStatus()==1){
                if(ObjectUtil.isNotEmpty(s.getAuditStatus()) && ObjectUtil.isNotEmpty(s.getUploadTime()) && ObjectUtil.isNotEmpty(s.getFinishTime())) {
                    t.setTaskTime(s.getAuditTime().getTime() - s.getUploadTime().getTime());
                    t.setAuditTime(s.getFinishTime().getTime() - s.getAuditTime().getTime());
                    valueList[2]=t.getTaskTime();
                    valueList[3]=t.getAuditTime();
                }
            }
        });
        //完成质量
        Task task = taskMapper.getOneTask(params);
        if(ObjectUtil.isNotEmpty(task) && StringUtils.isNotBlank(task.getEvaluate())) {
            t.setQuality(task.getEvaluate());
        }
        List<NameMax> nameList=new ArrayList<>();
        NameMax n1 = new NameMax();
        NameMax n2 = new NameMax();
        NameMax n3 = new NameMax();
        NameMax n4 = new NameMax();
        n1.setName("申请时长");
        n1.setMax("500");
        n2.setName("计划时长");
        n2.setMax("500");
        n3.setName("任务时长");
        n3.setMax("500");
        n4.setName("审核时长");
        n4.setMax("500");
        nameList.add(n1);
        nameList.add(n2);
        nameList.add(n3);
        nameList.add(n4);
        t.setNameList(nameList);
        t.setTimeList(valueList);
        return t;
    }
}
